package com.authManager.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.validation.annotation.Validated;
import org.apache.commons.lang3.StringUtils;

import com.authManager.entity.SysRole;
import com.authManager.entity.SysRoleMenu;
import com.authManager.entity.SysUserRole;
import com.authManager.server.service.ISysRoleMenuService;
import com.authManager.server.service.ISysRoleService;
import com.authManager.server.service.ISysUserRoleService;
import com.authManager.server.service.impl.SysUserRoleServiceImpl;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;

import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import java.util.HashMap;
import java.util.Map;
import java.util.Date;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.constraints.NotEmpty;
import org.hibernate.validator.constraints.Length;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

/**
 * <p>
 * 【角色信息】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-15
 */

@Api(value = "/admin/sysRole", tags = "【角色信息】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysRole", produces = BaseController.defProduces)
public class SysRoleController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysRoleController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysRoleService sysRoleService;
	@Autowired
	ISysUserRoleService sysUserRoleService;
	@Autowired
	ISysRoleMenuService sysRoleMenuService;
	
	@ApiOperation(value = "添加【角色信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "角色名", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "seq", value = "排序号", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "简介", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleCode", value = "角色编码", required = true), })
	@RequestMapping(value = "saveSysRole", method = { RequestMethod.POST })
	public ResultData<Object> saveSysRole(
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 100, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = true) Integer seq, @RequestParam(required = false) String description,
			@RequestParam(required = true) @NotEmpty(message = "参数[roleCode]不能为空") @Length(min = 0, max = 100, message = "参数[roleCode]最大长度不能超过{max}") String roleCode) {
		SysRole sysRole = new SysRole();
		if (StringUtils.isNotBlank(name)) {
			sysRole.setName(name);
		}
		if (seq != null) {
			sysRole.setSeq(seq);
		}
		if (StringUtils.isNotBlank(description)) {
			sysRole.setDescription(description);
		}
		if (StringUtils.isNotBlank(roleCode)) {
			SysRole sysRoleTmp = sysRoleService
					.selectOne(new EntityWrapper<SysRole>(new SysRole().setRoleCode(roleCode)));
			if (sysRoleTmp != null) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【roleCode】的值：【" + roleCode + "】已经存在").build();
			}
			sysRole.setRoleCode(roleCode);
		}
		sysRole.setCreater(getCurrUserLoginId());
		sysRole.setCreateTime(new Date());
		sysRole.setUpdater(null);
		sysRole.setUpdateTime(null);
		sysRoleService.insert(sysRole);
		Object sysRoleId = sysRole.getSysRoleId();
		return new ResultData.Builder<Object>(ResultCode.success).setData(sysRoleId).build();
	}

	@ApiOperation(value = "修改【角色信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleIdStr", value = "主键id", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "角色名", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "seq", value = "排序号", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "简介", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleCode", value = "角色编码", required = true), })
	@RequestMapping(value = "editSysRole", method = { RequestMethod.POST })
	public ResultData<Object> editSysRole(@RequestParam(required = true) Long sysRoleId,
			@RequestParam(required = true) @NotEmpty(message = "参数[name]不能为空") @Length(min = 0, max = 100, message = "参数[name]最大长度不能超过{max}") String name,
			@RequestParam(required = true) Integer seq, @RequestParam(required = false) String description,
			@RequestParam(required = true) @NotEmpty(message = "参数[roleCode]不能为空") @Length(min = 0, max = 100, message = "参数[roleCode]最大长度不能超过{max}") String roleCode) {
		SysRole sysRole = sysRoleService.selectById(sysRoleId);
		if (sysRole == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysRole = new SysRole();
		if (sysRoleId != null) {
			sysRole.setSysRoleId(sysRoleId);
		}
		if (StringUtils.isNotBlank(name)) {
			sysRole.setName(name);
		}
		if (seq != null) {
			sysRole.setSeq(seq);
		}
		if (StringUtils.isNotBlank(description)) {
			sysRole.setDescription(description);
		}
		if (StringUtils.isNotBlank(roleCode)) {
			SysRole sysRoleTmp = sysRoleService
					.selectOne(new EntityWrapper<SysRole>(new SysRole().setRoleCode(roleCode)));
			if (sysRoleTmp != null && sysRoleTmp.getSysRoleId() != sysRoleId) {
				return new ResultData.Builder<Object>(ResultCode.verifyUniqueness)
						.setMessage("参数：【roleCode】的值：【" + roleCode + "】已经存在").build();
			}
			sysRole.setRoleCode(roleCode);
		}
		// sysRole.setCreater(null);
		// sysRole.setCreateTime(null);
		sysRole.setUpdater(getCurrUserLoginId());
		sysRole.setUpdateTime(new Date());
		sysRoleService.updateById(sysRole);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【角色信息】")
	@RequestMapping(value = "delSysRole", method = { RequestMethod.POST })
	@Transactional
	public ResultData<Object> delSysRole(@RequestParam(required = true) Long sysRoleId) {
		SysRole sysRole = sysRoleService.selectById(sysRoleId);
		if (sysRole == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		
		int count = sysUserRoleService.selectCount(new EntityWrapper<>(new SysUserRole().setSysRoleId(sysRoleId)));
		if(count!=0) {
			return new ResultData.Builder<Object>(ResultCode.isNotNull).setMessage("已经有"+count+"位用户授权了此角色，请先取消授权。").build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		 sysRoleService.deleteById(sysRoleId);
		 sysRoleMenuService.delete(new EntityWrapper<>(new SysRoleMenu().setSysRoleId(sysRoleId)));
		// 逻辑删除
		// sysRole.setStatus(0);
		// sysRoleService.updateById(sysRoleId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【角色信息】")
	@RequestMapping(value = "getSysRole", method = { RequestMethod.POST })
	public ResultData<SysRole> getSysRole(@RequestParam(required = true) Long sysRoleId) {
		SysRole sysRole = sysRoleService.selectById(sysRoleId);
		if (sysRole == null) {
			return new ResultData.Builder<SysRole>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysRole>(ResultCode.success).setData(sysRole).build();
	}

	@ApiOperation(value = "列表【角色信息】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "主键id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "角色名", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "seq", value = "排序号", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "description", value = "简介", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "roleCode", value = "角色编码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "start_createTime", value = "开始-创建日期", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "end_createTime", value = "截至-创建日期", required = false), })
	@RequestMapping(value = "listSysRole", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysRole>> listSysRole(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysRoleId, @RequestParam(required = false) String name,
			@RequestParam(required = false) Integer seq, @RequestParam(required = false) String description,
			@RequestParam(required = false) String roleCode, @RequestParam(required = false) String start_creater,
			@RequestParam(required = false) String end_creater) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键id
		if (sysRoleId != null) {
			paramMap.put("sysRoleId", sysRoleId);
		}
		// 角色名
		if (StringUtils.isNotBlank(name)) {
			paramMap.put("name", name);
		}
		// 排序号
		if (seq != null) {
			paramMap.put("seq", seq);
		}
		// 简介
		if (StringUtils.isNotBlank(description)) {
			paramMap.put("description", description);
		}
		// 角色编码
		if (StringUtils.isNotBlank(roleCode)) {
			paramMap.put("roleCode", roleCode);
		}
		// 开始-创建人
		if (StringUtils.isNotBlank(start_creater)) {
			paramMap.put("start_creater", start_creater);
		}
		// 结束-创建人
		if (StringUtils.isNotBlank(end_creater)) {
			paramMap.put("end_creater", end_creater);
		}
		return sysRoleService.listSysRole(paramMap);
	}
}
